local skel = fk.CreateSkill {
  name = "emo__shancai",
}

Fk:loadTranslationTable{
  ["emo__shancai"] = "椫材",
  [":emo__shancai"] = "你可以将两张牌当任意普通锦囊牌使用，令此技能失效至后X个回合结束（X为此两牌点数差）；“椫材”下次生效时，你可再使用一张非装备牌并摸一张牌。",

  ["@emo__shancai"] = "椫材",
  ["#emo__shancai-use"] = "椫材：你可使用一张非装备牌，并摸一张牌",
  ["#emo__shancai"] = "椫材：你可以将两张牌当任意普通锦囊牌使用",

  ["$emo__shancai1"] = "",
  ["$emo__shancai2"] = "",
}

skel:addEffect("viewas", {
  mute_card = false,
  prompt = "#emo__shancai",
  pattern = ".|.|.|.|.|trick",
  interaction = function(self, player)
    local all_choices = Fk:getAllCardNames("t")
    local choices = player:getViewAsCardNames(skel.name, all_choices)
    if #choices > 0 then
      return UI.CardNameBox { choices = choices, all_choices = all_choices }
    end
  end,
  handly_pile = true,
  filter_pattern = {
    min_num = 2,
    max_num = 2,
    pattern = ".",
  },
  card_filter = function (self, player, to_select, selected)
    return #selected < 2
  end,
  view_as = function (self, player, cards)
    if #cards ~= 2 or Fk.all_card_types[self.interaction.data] == nil then return nil end
    local c = Fk:cloneCard(self.interaction.data)
    c.skillName = skel.name
    c:addSubcards(cards)
    return c
  end,
  before_use = function(self, player, use)
    local room = player.room
    local cards = use.card.subcards
    if #cards ~= 2 then return "" end
    local num = math.abs(Fk:getCardById(cards[1]).number - Fk:getCardById(cards[2]).number)
    if num > 0 then
      room:invalidateSkill(player, skel.name)
      room:setPlayerMark(player, "@emo__shancai", num)
    end
  end,
  enabled_at_play = function(self, player)
    return true
  end,
  enabled_at_response = function (self, player, response)
    return not response
  end,
})

skel:addEffect(fk.TurnEnd, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name, true) and player:getMark("@emo__shancai") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:removePlayerMark(player, "@emo__shancai")
    if player:getMark("@emo__shancai") == 0 then
      room:notifySkillInvoked(player, skel.name, "special")
      room:validateSkill(player, skel.name)
      if room:askToPlayCard(player, {
        pattern = ".|.|.|.|.|trick,basic", skill_name = skel.name, prompt = "#emo__shancai-use", skip = false
      }) and not player.dead then
        player:drawCards(1, skel.name)
      end
    end
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "emo__shancai", 0)
end)

return skel
